package com.xkdx.guangguang.fragment.shopinfo;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.DiscountInfo;
import com.xkdx.guangguang.shareclass.ShopCommentary;
import com.xkdx.guangguang.shareclass.ShopDetail;

public class ShopInfoModule extends AbsModule {
	ShopDetail shopDetail;
	List<DiscountInfo> newInfoList;
	List<ShopCommentary> shopCommentList;
	String shopCommentNum = "0";

	@Override
	public void parseData() throws Exception {

		try {
			backJson = (JSONArray) result;

			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("getShopDetail")) {
					shopDetail = parseShopInfoDetail(jsonObj
							.getJSONObject("Content"));
				} else if (sortOfAct.equals("getInfoListByShop")) {
					newInfoList = parseDiscountInfo(jsonObj
							.getJSONObject("Content"));
				} else {
					shopCommentList = parseAllCommentInfo(jsonObj
							.getJSONObject("Content"));
				}

			}
		} catch (Exception e) {
			// to do define a exception include all parseException
			throw e;
		}

	}

	// 解析评论详情内容
	private List<ShopCommentary> parseAllCommentInfo(JSONObject person)
			throws Exception {
		List<ShopCommentary> list = new ArrayList<ShopCommentary>();
		try {
			JSONObject stut = (JSONObject) person.get("Result");
			if (stut.has("DetailInfo")) {
				JSONObject detail = stut.getJSONObject("DetailInfo");
				shopCommentNum = detail.getString("DataCount");

			}
			if (stut.has("ListInfo")) {
				JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");
				for (int i = 0; i < ListInfo.length(); i++) {
					JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
					ShopCommentary commentinfo = new ShopCommentary();
					commentinfo.setCommentContent(jsonObj
							.getString("CommentContent"));
					commentinfo.setNickName(jsonObj.getString("NickName"));
					commentinfo.setCreateTime(jsonObj.getString("CreateTime"));
					commentinfo.setPictrueUrl(jsonObj.getString("PictureUrl"));
					commentinfo
							.setUserAvatars(jsonObj.getString("UserAvatars"));
					list.add(commentinfo);
				}
			}

		} catch (Exception e) {
			throw e;
		}

		return list;
	}

	private ShopDetail parseShopInfoDetail(JSONObject person) throws Exception {

		ShopDetail detail = new ShopDetail();
		List<String> list = new ArrayList<String>();
		try {
			JSONObject stuts = (JSONObject) person.get("Result");
			JSONObject item = stuts.getJSONObject("DetailInfo");

			detail.setAddress(item.getString("Address"));
			detail.setIntro(item.getString("Intro"));
			detail.setLatitude(item.getString("Latitude"));
			detail.setLogo(item.getString("Logo"));
			detail.setLongitude(item.getString("Longitude"));
			detail.setSimpleintro(item.getString("SimpleIntro"));
			detail.setShopID(item.getString("ShopID"));
			detail.setShopName(item.getString("ShopName"));
			detail.setWebSiteUrl(item.getString("WebSiteUrl"));
			detail.setDiscountCount(item.getString("DiscountCount"));
			detail.setAttentionCount(item.getString("AttentionCount"));
			detail.setChildShopCount(item.getString("ChildShopCount"));
			detail.setCommentCount(item.getString("CommentCount"));
			detail.setNewProductCount(item.getString("NewProductCount"));
			detail.setPhone(item.getString("Phone"));
			detail.setSignInCount(item.getString("SignInCount"));
			detail.setShopType(item.getString("ShopType"));
			detail.setWeibo(item.getString("Weibo"));
			detail.setDianpingShopID(item.getString("DianpingShopID"));
			detail.setIcon1(item.getString("Icon1"));
			detail.setIcon2(item.getString("Icon2"));
			detail.setIcon3(item.getString("Icon3"));

			JSONArray array = item.getJSONArray("PictureList");
			for (int i = 0; i < array.length(); i++) {
				String l = (String) array.optString(i);
				list.add(l);
			}
			detail.setPictureList(list);

		} catch (Exception e) {
			throw e;
		}

		return detail;
	}

	private List<DiscountInfo> parseDiscountInfo(JSONObject person)
			throws Exception {
		List<DiscountInfo> list = new ArrayList<DiscountInfo>();

		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");
			JSONObject time = stut.getJSONObject("DetailInfo");
			for (int i = 0; i < ListInfo.length(); i++) {
				JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
				DiscountInfo discountInfo = new DiscountInfo();
				discountInfo.setInfoID(jsonObj.getString("InfoID"));
				discountInfo.setInfoTitle(jsonObj.getString("InfoTitle"));
				discountInfo.setInfoType(jsonObj.getString("InfoType"));
				discountInfo.setBeginTime(jsonObj.getString("BeginTime"));
				discountInfo.setEndTime(jsonObj.getString("EndTime"));
				discountInfo.setPictrueUrl(jsonObj.getString("PictureUrl"));
				list.add(discountInfo);
			}

		} catch (Exception e) {
			throw e;
		}

		return list;

	}

}
